class Solution {
public:
    int totalFruit(vector<int>& a) {
        int n = a.size();
        int ans = 0;
        unordered_map<int, int>mp;
        int l = 0;
        for (int i = 0; i < n; i++) {
            mp[a[i]]++;
            while (mp.size() >= 3) {
                mp[a[l]]--;
                if (mp[a[l]] == 0)mp.erase(a[l]);
                l++;
            }
            ans = max(ans, i - l + 1);
        }
        return ans;
    }
};